﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Goods_SaleWay_Discount
{
    public partial class Form : FormPage
    {
        BLL.Goods_SaleWay_Discount bll = new BLL.Goods_SaleWay_Discount();
        Model.Goods_SaleWay_Discount model = new Model.Goods_SaleWay_Discount();
        //
        int GoodsID = 0;
        string DateFormat = "yyyy 年 MM 月 dd 日";
        //
        protected bool isAdmin = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTypePlus.getPageRequest(Request.QueryString["GoodsID"], ref GoodsID);
            isAdmin = Security.GetCurrentUserType() == EnumHelper.UserType.Administrator;
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (!isAdmin) group_Sell.Visible = false;
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Money.Text = model.Money.ToString();
            tb_DiscountBuy.Text = (Convert.ToDecimal( model.DiscountBuy) / 10).ToString();
            tb_DiscountSell.Text = (Convert.ToDecimal(model.DiscountSell) / 10).ToString();
            tb_DateFrom.Text = Convert.ToDateTime(model.DateFrom).ToString(DateFormat);
            tb_DateTo.Text = Convert.ToDateTime(model.DateTo).ToString(DateFormat);
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            decimal Money = DataTypePlus.requestDecimal(tb_Money.Text);
            int DiscountBuy = Convert.ToInt32((Convert.ToDecimal(tb_DiscountBuy.Text) * 10));
            int DiscountSell = Convert.ToInt32((Convert.ToDecimal(tb_DiscountSell.Text) * 10));
            DateTime DateFrom = DateTime.ParseExact(tb_DateFrom.Text, DateFormat, System.Globalization.CultureInfo.InvariantCulture);
            DateTime DateTo = DateTime.ParseExact(tb_DateTo.Text, DateFormat, System.Globalization.CultureInfo.InvariantCulture);
            if (
                Master.noZero(Money, "[购买金额]必须填写") &&
                Master.noChongfu("Money='" + Money + "' and GoodsID=" + GoodsID, "[购买金额]重复")
                )
            {
                model.GoodsID = GoodsID;
                model.Money = Money;
                model.DiscountBuy = DiscountBuy;
                if (isAdmin) model.DiscountSell = DiscountSell;
                model.DateFrom = DateFrom;
                model.DateTo = DateTo;
                if (Master.formState == EnumHelper.FormState.add)Master.id= bll.Add(model);
                else bll.Update(model);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_SaleWay_Discount, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                Master.SubmitOK();

            }
        }
    }
}